<?php

namespace frontend\modules\dev\controllers;

use Yii;
use yii\db\Query;
use frontend\models\record\PostCategory;
use frontend\models\PageNavi;
use common\libs\dsyog\util\UploadMan;

/**
 * 分类
 */
class CategoryController extends BaseController
{

    /**
     * Renders the index view for the module
     * @return string
     */
    public function actionIndex()
    {
        /**
        $uploadMan = new UploadMan();
        $uploadMan->initParams();
        $pathWebRoot = Yii::getAlias('@webroot');
        $tmpFile = $pathWebRoot . '/assets/a.txt';
        $uploadMan->storageUploadByPath($tmpFile);
         */

        $count = (new Query())->select('COUNT(*)')
            ->from(PostCategory::tableName())
            ->createCommand(Yii::$app->db)
            ->queryScalar();

        $url = Yii::$app->request->getHostInfo() . '/dev/category/index';
        $pageNavi = new PageNavi();
        $pageNavi->initParams(10, $count, $url);

        $category_list = (new Query())->select('*')
            ->from(PostCategory::tableName())
            ->offset($pageNavi->offset)
            ->limit($pageNavi->limit)
            ->createCommand(Yii::$app->db)
            ->queryAll();
        return $this->render('index', [
            'list' => $category_list,
            'pageNavi' => $pageNavi,
        ]);
    }

    public function actionCreate()
    {
        return $this->render('create', [
        ]);
    }

    public function actionAjaxcreate()
    {
        $model = new PostCategory();
        $model->load(Yii::$app->request->post(), '');
        $error_array = $model->handleSubmit();
        return json_encode($error_array);
    }
}
